<?php
namespace Home\Controller;
use Think\Controller;

/**
 * 总后台登陆
 */
class PageController extends Controller {
    
    public $userInfo;
    public $tooken = 'lkasdj213489ulkjadsf';
    
    public function __construct() {
        parent::__construct();
        //exit;
        //引入公共微信分享类库
		import("Org.WechatJsSDK.JSSDK");
        
        $userInfo = session('userInfo');
//        if(empty($userInfo['wx_id'])){
//            //$goUrl = \Org\Net\Fun::url('home/index/index');
//            //header("Location:".$goUrl);exit;
//			$returnData = array();
//			$returnData['state'] = 0;
//			$returnData['message'] = 'no userInfo';
//			echo json_encode($returnData);exit;
//        }else {
            $this->userInfo = $userInfo;
//        }
            
         if(!empty($this->userInfo['wx_id'])){
            $userModel = M('User');
            $userDATA = $userModel->where(array('wx_id'=>$this->userInfo['wx_id']))->find();
            $updateData = array();
            $updateData['onclick'] = $userDATA['onclick']+1;
            $userModel->where(array('wx_id'=>$this->userInfo['wx_id']))->save($updateData); // 根据条件更新记录
        }   
            
            
    }


    //活动首页
    public function index(){
		$userModel = M('User');
		$BanquetModel = M('Banquet');
        if(empty($this->userInfo['wx_id'])){
            $goUrl = \Org\Net\Fun::url('home/index/index');
            header("Location:".$goUrl);exit;	
        }
		$wx_id = $this->userInfo['wx_id'];
        $whereBan = array('wx_id'=>$wx_id);
		$banqueData = $BanquetModel->where($whereBan)->find();
		$this->assign('banqueData', $banqueData); //js的json数据
		$this->assign('BanquetInfo', $banqueData); //宴会数据
        $this->assign('userInfo', $this->userInfo); //用户数据
		$whereToBanquet = "wx_id='{$wx_id}'";
		$BanquetInfo = $BanquetModel->where($whereToBanquet)->find();
		$this->assign('BanquetUserInfo', $BanquetUserInfo); //宴会主人数据
		
        $this->display('index');
    }
    
    /**
     * 详情页面
     */
    public function information(){
		$userModel = M('User');
        $BanquetModel = M('Banquet');
        if(empty($this->userInfo['wx_id'])){
            $goUrl = \Org\Net\Fun::url('home/index/index');
            header("Location:".$goUrl);exit;	
        }
        $wx_id = $this->userInfo['wx_id'];
        $whereBan = array('wx_id'=>$wx_id);
        $banqueData = $BanquetModel->where($whereBan)->find();
        $this->assign('banqueData', $banqueData); //js的json数据
		$this->assign('BanquetInfo', $banqueData); //宴会数据
        $this->assign('userInfo', $this->userInfo); //用户数据
		$whereToBanquet = "wx_id='{$wx_id}'";
		$BanquetInfo = $BanquetModel->where($whereToBanquet)->find();
		$whereToUser = array('wx_id'=>$BanquetInfo['wx_id']);
        $BanquetUserInfo = $userModel->where($whereToUser)->find();
		$this->assign('BanquetUserInfo', $BanquetUserInfo); //宴会主人数据
		
        $this->display('information');
    }


    /**
     * 创办酒席
     */
    public function createBanquet(){
        if(empty($this->userInfo['wx_id'])){
            $goUrl = \Org\Net\Fun::url('home/index/index');
            header("Location:".$goUrl);exit;	
        }
        
        $BanquetModel = M('Banquet');
        $tableModel = M('Table');
        $ParticipateModel = M('Participate');
//        
//        $tooken = $this->tooken;
//        $postDataJson = $_POST['qwer'];
//        $key = $_POST['session_id'];
//        $returnData = array();
//        if(md5($postDataJson.$this->tooken)!=$key){
//            $returnData['message'] = '非法请求';
//            $returnData['state'] = 0;
//            echo json_encode($returnData);exit;
//        } else {
//            $psotDataObjec = json_decode($postDataJson);
//            $phone = $psotDataObjec->phone;
//            $name = $psotDataObjec->name;
//            $address = $psotDataObjec->address;
//            $banquet_date = $psotDataObjec->banquet_date;
//            if(empty($phone)||empty($name)||empty($address)||empty($banquet_date)){
//                $returnData['message'] = '请填写完整您的信息';
//                $returnData['state'] = 0;
//                echo json_encode($returnData);exit;
//            }
//            
//            //判断当前用户是否是已经创建了酒席
//            $wx_id = $this->userInfo['wx_id'];
//            $whereToBanquet = "wx_id='{$wx_id}'";
//            $BanquetInfo = $BanquetModel->where($whereToBanquet)->find();
//
//            //如果没有则创建一个宴会，并且设置一个桌面
//            if(!$BanquetInfo['id']){
//                $saveBanquetData = array();
//                
//                $saveBanquetData['name'] = $name;
//                $saveBanquetData['phone'] = $phone;
//                $saveBanquetData['address'] = $address;
//                $saveBanquetData['banquet_date'] = $banquet_date;
//                
//                $saveBanquetData['wx_id'] = $wx_id;
//                $saveBanquetData['wx_name'] = $this->userInfo['name'];
//                $saveBanquetData['image'] = $this->userInfo['image'];
//                $saveBanquetData['people'] = 1;
//                $saveBanquetData['add_time'] = \Org\Net\Fun::now_time();
//                $banquetId = $BanquetModel->add($saveBanquetData);
//
//                //保存一个主席桌子
//                $saveTable = array();
//                $saveTable['table_name'] = "主宾桌";
//                $saveTable['banquet_id'] = $banquetId;
//                $saveTable['is_main'] = 1;
//                $saveTable['position'] = 0;
//                $saveTable['add_time'] = \Org\Net\Fun::now_time();
//                $tableId = $tableModel->add($saveTable);
//
//                //循环五个桌子出来，至少五个桌子 begin
//                $inserTbaleAllArr = array();
//                for($i=1;$i<=5;$i++){
//                    $tableArr = array();
//                    $tableArr['table_name'] = '第'.$i.'桌';
//                    $tableArr['banquet_id'] = $banquetId;
//                    $tableArr['is_main'] = 0;
//                    $tableArr['position'] = $i;
//                    $tableArr['add_time'] = \Org\Net\Fun::now_time();
//
//                    $inserTbaleAllArr[] = $tableArr;
//                }
//                $tableModel->addAll($inserTbaleAllArr);
//                //循环五个桌子出来，至少五个桌子 end
//
//                //将自己的信息加入到第一个桌子
//                $saveParticipateData = array();
//                $saveParticipateData['table_id'] = $tableId;
//                $saveParticipateData['banquet_id'] = $banquetId;
//                $saveParticipateData['position'] = 1;
//                $saveParticipateData['wx_id'] = $wx_id;
//                $saveParticipateData['name'] = $this->userInfo['name'];
//                $saveParticipateData['image'] = $this->userInfo['image'];
//                $saveParticipateData['add_time'] = \Org\Net\Fun::now_time();
//                $ParticipateModel->add($saveParticipateData);
//                
//                $reMessage = '提交成功';
//            } else {
//                $banquetId = $BanquetInfo['id'];
//                $reMessage = '您已经提交过';
//            }
//            $url = \Org\Net\Fun::url('home/page/table',array('id'=>$banquetId));
//            
//            $returnData = array();
//            $returnData['message'] = $reMessage;
//            $returnData['state'] = 1;
//            $returnData['url'] = $url;
//            echo json_encode($returnData);exit;
		
		

            
        //判断当前用户是否是已经创建了酒席
		$wx_id = $this->userInfo['wx_id'];
		if(empty($wx_id)){
			echo 'Fei Fa Qing Qiu';exit;
		}
		
		$whereToBanquet = "wx_id='{$wx_id}'";
		$BanquetInfo = $BanquetModel->where($whereToBanquet)->find();

		//如果没有则创建一个宴会，并且设置一个桌面
		if(!$BanquetInfo['id']){
			$saveBanquetData = array();
			
//			$saveBanquetData['name'] = $name;
//			$saveBanquetData['phone'] = $phone;
//			$saveBanquetData['address'] = $address;
//			$saveBanquetData['banquet_date'] = $banquet_date;

			$saveBanquetData['wx_id'] = $wx_id;
			$saveBanquetData['wx_name'] = $this->userInfo['name'];
			$saveBanquetData['image'] = $this->userInfo['image'];
			$saveBanquetData['people'] = 1;
			$saveBanquetData['add_time'] = \Org\Net\Fun::now_time();
			$banquetId = $BanquetModel->add($saveBanquetData);

			//保存一个主席桌子
			$saveTable = array();
			$saveTable['table_name'] = "主宾桌";
			$saveTable['banquet_id'] = $banquetId;
			$saveTable['is_main'] = 1;
			$saveTable['position'] = 0;
			$saveTable['add_time'] = \Org\Net\Fun::now_time();
			$tableId = $tableModel->add($saveTable);

			//循环五个桌子出来，至少五个桌子 begin
			$inserTbaleAllArr = array();
			for($i=1;$i<=5;$i++){
				$tableArr = array();
				$tableArr['table_name'] = '第'.$i.'桌';
				$tableArr['banquet_id'] = $banquetId;
				$tableArr['is_main'] = 0;
				$tableArr['position'] = $i;
				$tableArr['add_time'] = \Org\Net\Fun::now_time();

				$inserTbaleAllArr[] = $tableArr;
			}
			$tableModel->addAll($inserTbaleAllArr);
			//循环五个桌子出来，至少五个桌子 end

			//将自己的信息加入到第一个桌子
			$saveParticipateData = array();
			$saveParticipateData['table_id'] = $tableId;
			$saveParticipateData['banquet_id'] = $banquetId;
			$saveParticipateData['position'] = 1;
			$saveParticipateData['wx_id'] = $wx_id;
			$saveParticipateData['name'] = $this->userInfo['name'];
			$saveParticipateData['image'] = $this->userInfo['image'];
			$saveParticipateData['add_time'] = \Org\Net\Fun::now_time();
			$ParticipateModel->add($saveParticipateData);
            
            
		} else {
			$banquetId = $BanquetInfo['id'];
		}
        $url = \Org\Net\Fun::url('home/page/table',array('id'=>$banquetId,'isWeb'=>1));
        header("Location:".$url);exit;
    }
    
    /**
     * 更新宴会主要信息
     */
    public function updateBanquet(){

        $returnData = array();
        $returnData['message'] = '活动已经结束';
        $returnData['state'] = 0;
        echo json_encode($returnData);exit;    



        if(empty($this->userInfo['wx_id'])){
            $returnData['message'] = '非法请求';
            $returnData['state'] = 0;
            echo json_encode($returnData);exit;
        }
        $wx_id = $this->userInfo['wx_id'];
        $BanquetModel = M('Banquet');
        $tableModel = M('Table');
        $ParticipateModel = M('Participate');
        
        $tooken = $this->tooken;
        $postDataJson = $_POST['qwer'];
        $key = $_POST['session_id'];
        $returnData = array();
        
        if(md5($postDataJson.$this->tooken)!=$key){
            $returnData['message'] = '非法请求';
            $returnData['state'] = 0;
            echo json_encode($returnData);exit;
        } else {
            $psotDataObjec = json_decode($postDataJson);
            $phone = $psotDataObjec->phone;
            $name = $psotDataObjec->name;
            $address = $psotDataObjec->address;
            $banquet_date = $psotDataObjec->banquet_date;
           if(empty($phone)||empty($name)||empty($address)||empty($banquet_date)){
                $returnData['message'] = '请填写完整您的信息';
                $returnData['state'] = 0;
                echo json_encode($returnData);exit;
            }
            
            
            //找到对应的宴会，如果没有对应的宴会，则无法操作这一步
            $whereBan = array('wx_id'=>$wx_id);
            $banqueData = $BanquetModel->where($whereBan)->find();
            
			//如果填写过就不能再填写
			if(!empty($banqueData['name']) && !empty($banqueData['phone']) && !empty($banqueData['address']) && !empty($banqueData['banquet_date'])){
				$returnData['message'] = '您已经填写过信息了，不用再填了';
                $returnData['state'] = 2;
                echo json_encode($returnData);exit;
			}
			
			
			
            if(!$banqueData){
                $returnData['message'] = '你还没有宴会哦';
                $returnData['state'] = 2;
                echo json_encode($returnData);exit;
            } else {
                $saveData = array();
                $saveData['name'] = $name;
                $saveData['phone'] = $phone;
                $saveData['address'] = $address;
                $saveData['banquet_date'] = $banquet_date;
                $saveData['is_calculation'] = 1;
				$saveData['is_first'] = 0;
                $BanquetModel->where(array('wx_id'=>$wx_id))->save($saveData);
                
                $returnData['message'] = '提交成功';
                $returnData['state'] = 1;
                echo json_encode($returnData);exit;
            }
        }
    }




    /**
     * 显示 某个宴会的桌子
     */
    public function table(){
        
        $id = I("get.id");
        $isWeb = I("get.isWeb");
        $id = intval($id);
        $isWeb = $isWeb?$isWeb:0;
        $BanquetModel = M('Banquet');
        $tableModel = M('Table');
        $ParticipateModel = M('Participate');
        $userModel = M('User');
		
		
		
        $BanquetInfo = $BanquetModel->where("id={$id}")->find();
        $wx_id = $this->userInfo['wx_id'];
		
		if(!$BanquetInfo['id']>0){
			$url = \Org\Net\Fun::url('home/index/index');
			header("Location:".$url);exit;
		}
		
        
        //查询宴会主人的信息
        $whereToUser = array('wx_id'=>$BanquetInfo['wx_id']);
        $BanquetUserInfo = $userModel->where($whereToUser)->find();
        
        //如果是朋友圈进来的用户，需要先回到用首页进行跳转到首页
        if(empty($this->userInfo['wx_id'])){
            $goUrl = \Org\Net\Fun::url('home/index/test');
            session('callUrl',\Org\Net\Fun::url('home/page/table',array('id'=>$id)));
            header("Location:".$goUrl);exit;	
        }
        
        //如果存在宴会
        if($BanquetInfo){
            //查询宴会桌子
            $where = "banquet_id='{$id}'";
            $tableData = $tableModel->where($where)->select();
            
            //查询对应的参与人数
            foreach($tableData as $key=>$item){
                $tableId = $item['id'];
                $parData = array();
                $parData = $ParticipateModel->where("table_id='{$tableId}'")->select();
                $tableData[$key]['participateData'] = $parData;
            }
            
            //生成json格式数据
            $jsonData = array();
			$peopleNumber = 0;
			$tableNumber = 0;
            foreach($tableData as $key=>$item){
				
               $arr = array();
			   $countArr = array();
                for($i=0;$i<6;$i++){
                    $new = array();
                    $new['id'] = "第".($i+1)."位";
                    $new['name'] = "";
                    $new['url'] = "/Public/game/images/seated_bg.png";
                    $arr[$i] = $new;
                    $hasEmpty = 1;
                }
				
				foreach($item['participateData'] as $kk=>$vv){
                    $new = array();
					$key = $vv['position']-1;
                    $new['id'] = "第".$vv['position']."位";
                    $new['name'] = $vv['name'];
                    $new['url'] = $vv['image'];
					$countArr = $new;
                    $arr[$key] = $new;
					++$peopleNumber; //计算入席人数
                }
				
				++$tableNumber;  //计算桌子数
                $jsonData[] = $arr;
            }
			
            $g_tableNumber = $tableNumber*6;
			
            //如果座位都坐满了，那么就需要加多一个桌子，然后再入到数据库
            if($g_tableNumber==$peopleNumber){
                $whreTable = array("banquet_id"=>$id);
                $count = $tableModel->where($whreTable)->max('position'); //目前有多少桌子
                $saveNewTable = array();
                $saveNewTable['table_name'] = '第'.($count+1).'桌';
                $saveNewTable['is_main'] = 0;
                $saveNewTable['position'] = $count+1;
                $saveNewTable['banquet_id'] = $id;
                $saveNewTable['add_time'] = \Org\Net\Fun::now_time();;
                $newTableId = $tableModel->add($saveNewTable);
                
                $newTableData = array();
                for($i=1;$i<=6;$i++){
                    $new = array();
                    $new['id'] = "第".$i."位";
                    $new['name'] = "";
                    $new['url'] = "/Public/game/images/seated_bg.png";
                    $newTableData[] = $new;
                }
                $jsonData[] = $newTableData;
            }
            //print_r($jsonData);exit;
            $jsonDataObj = json_encode($jsonData);
        } 
        
        
        
        $this->assign('mainTableData', $tableData[0]); // 主宾数据
        $listTableData = $tableData;
        unset($listTableData[0]);
        $this->assign('listTableData', $listTableData); // 普通桌席数据
        $this->assign('jsonDataObj', $jsonDataObj); //js的json数据
        $this->assign('BanquetInfo', $BanquetInfo); //宴会数据
        $this->assign('userInfo', $this->userInfo); //用户数据
        //当前用户的json
        $this->assign('id', $id);
        $this->assign('BanquetUserInfo', $BanquetUserInfo); //宴会主人数据
		
		if($BanquetInfo['is_first']==0){
			$updateArr = array();
			$updateArr['is_first'] = 1;
			$BanquetModel->where("id={$id}")->save($updateArr);
			$isWeb = 1;
		} else {
			$isWeb = 0;
		}
        $this->assign('isWeb', $isWeb);
         
         
         
        //是否可以再次添加 begin
        $pString = "banquet_id='{$id}' AND wx_id='{$wx_id}'";
        $pData = $ParticipateModel->where($pString)->find();
        if($pData){
            $this->assign('hasP', 1);
        } else {
            $this->assign('hasP', 0);
        }
        //是否可以再次添加 end;
		
		
		//我的参与 begin
		$wherePar = array();
		$wherePar['wx_id'] = $wx_id;
		$wherePar['banquet_id'] = $id;
        $mePar = $ParticipateModel->where($wherePar)->find();
        $this->assign('mePar', $mePar); 
		//我的参与 end
		
		//我的桌子 begin
		$wherePar = array();
		$wherePar['id'] = $mePar['table_id'];
		$wherePar['banquet_id'] = $id;
        $meTable = $tableModel->where($wherePar)->find();
        $this->assign('meTable', $meTable); 
		//我的桌子 end
		
		 
        $this->display('createBanquet');
    }
    
    /**
     * 加入桌位
     */
    public function addParticipate(){

        $returnData = array();
        $returnData['message'] = '活动已经结束';
        $returnData['state'] = 0;
        echo json_encode($returnData);exit;  


        $BanquetModel = M('Banquet');
        $tableModel = M('Table');
        $ParticipateModel = M('Participate');
        
        $tooken = $this->tooken;
        $postDataJson = $_POST['qwer'];
        $key = $_POST['session_id'];
        $returnData = array();
        if(md5($postDataJson.$this->tooken)!=$key){
            $returnData['message'] = '非法请求';
            $returnData['state'] = 0;
            echo json_encode($returnData);exit;
        } else {
            //验证通过
            $psotDataObjec = json_decode($postDataJson);
            $banquet_id = $psotDataObjec->banquet_id;
            $table_id = $psotDataObjec->table_id;
            $position = $psotDataObjec->position;
			$position = intval($position)+1;
            $wx_id = $this->userInfo['wx_id'];
            
            //判断用户是否在这个,宴会的位置上，如果已经参加了这个宴会，那么就不能再参加了
            $pString = "banquet_id='{$banquet_id}' AND wx_id='{$wx_id}'";
            $pData = $ParticipateModel->where($pString)->find();
            if($pData){
                $returnData['message'] = '您已经参加该宴会了哦';
                $returnData['state'] = 0;
                echo json_encode($returnData);exit;
            }
            
            //查看这个位置是否有人选过，如果有人选了这个位置，不能再选这个位置了
            $pString_Two = "banquet_id='{$banquet_id}' AND table_position='{$table_id}' AND position='{$position}'";
            $pDataTwo = $ParticipateModel->where($pString_Two)->find();
            if($pDataTwo){
                $returnData['message'] = '您来迟一步了，该位置已经被选走了';
                $returnData['state'] = 0;
                echo json_encode($returnData);exit;
            }
			//查询会table表的信息
			$whereToTable = "position='{$table_id}' AND banquet_id='{$banquet_id}'";
			$tableInfo =$tableModel->where($whereToTable)->find();
           
            //如果没有参加这个宴会则进行 加入数据操作
			if($tableInfo['id']>0){
				$saveP = array();
				$saveP['table_id'] = $tableInfo['id'];
				$saveP['table_position'] = $table_id;
				$saveP['wx_id'] = $wx_id;
				$saveP['name'] = $this->userInfo['name'];
				$saveP['image'] = $this->userInfo['image'];
				$saveP['banquet_id'] = $banquet_id;
				$saveP['position'] = $position;
				$saveP['add_time'] = \Org\Net\Fun::now_time();
				$inserId = $ParticipateModel->add($saveP);

				//统计参与人数，更新到宴会表
				$whereString = "banquet_id='{$banquet_id}'";
				$pCount = $ParticipateModel->where($whereString)->count(); //查询参与宴会人数
				$updateData = array();
				$updateData['people'] = $pCount;
				$BanquetModel->where('id='.$banquet_id)->save($updateData);
				
			} else {
				$returnData['message'] = '加入失败';
                $returnData['state'] = 0;
                echo json_encode($returnData);exit;
			}
            
            
            $returnData['message'] = '参与成功';
            $returnData['state'] = 1;
            echo json_encode($returnData);exit;
        }
        
    }
    
    
    /**
     * 免费领取酒席
     */
    public function freeOfCharge(){
		$userModel = M('User');
		$BanquetModel = M('Banquet');
        $wx_id = $this->userInfo['wx_id'];
        $whereBan = array('wx_id'=>$wx_id);
        $banqueData = $BanquetModel->where($whereBan)->find();
        $this->assign('banqueData', $banqueData); //js的json数据
		$this->assign('BanquetInfo', $banqueData); //宴会数据
        $this->assign('userInfo', $this->userInfo); //用户数据
		$whereToBanquet = "wx_id='{$wx_id}'";
		$BanquetInfo = $BanquetModel->where($whereToBanquet)->find();
		$whereToUser = array('wx_id'=>$BanquetInfo['wx_id']);
        $BanquetUserInfo = $userModel->where($whereToUser)->find();
		$this->assign('BanquetUserInfo', $BanquetUserInfo); //宴会主人数据
		
        $exchangeModel = M('Exchange');
        $exchange_codeModel = M('Exchange_code');
        
        
        $countArr = array();
        //梅州
        $inId = $exchangeModel->where("city_cd='Mz'")->getField("GROUP_CONCAT(id)");
        $countArr['Mz'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        
        //潮州
        $inId = $exchangeModel->where("city_cd='Cz'")->getField("GROUP_CONCAT(id)");
        $countArr['Cz'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        
        //揭阳
        $inId = $exchangeModel->where("city_cd='Jy'")->getField("GROUP_CONCAT(id)");
        $countArr['Jy'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //汕头
        $inId = $exchangeModel->where("city_cd='St'")->getField("GROUP_CONCAT(id)");
        $countArr['St'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //河源
        $inId = $exchangeModel->where("city_cd='Hy'")->getField("GROUP_CONCAT(id)");
        $countArr['Hy'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        
        //汕尾
        $inId = $exchangeModel->where("city_cd='Sw'")->getField("GROUP_CONCAT(id)");
        $countArr['Sw'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        
        //韶关
        $inId = $exchangeModel->where("city_cd='Sg'")->getField("GROUP_CONCAT(id)");
        $countArr['Sg'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        
        //惠州
        $inId = $exchangeModel->where("city_cd='Hz'")->getField("GROUP_CONCAT(id)");
        $countArr['Hz'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //清远
        $inId = $exchangeModel->where("city_cd='Qy'")->getField("GROUP_CONCAT(id)");
        $countArr['Qy'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //广州
        $inId = $exchangeModel->where("city_cd='Gz'")->getField("GROUP_CONCAT(id)");
        $countArr['Gz'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //东莞
        $inId = $exchangeModel->where("city_cd='Dg'")->getField("GROUP_CONCAT(id)");
        $countArr['Dg'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //深圳
        $inId = $exchangeModel->where("city_cd='Sz'")->getField("GROUP_CONCAT(id)");
        $countArr['Sz'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        
        //肇庆
        $inId = $exchangeModel->where("city_cd='Zq'")->getField("GROUP_CONCAT(id)");
        $countArr['Zq'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        
        //佛山
        $inId = $exchangeModel->where("city_cd='Fs'")->getField("GROUP_CONCAT(id)");
        $countArr['Fs'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //中山
        $inId = $exchangeModel->where("city_cd='Zs'")->getField("GROUP_CONCAT(id)");
        $countArr['Zs'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //珠海
        $inId = $exchangeModel->where("city_cd='Zh'")->getField("GROUP_CONCAT(id)");
        $countArr['Zh'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //云浮
        $inId = $exchangeModel->where("area_cd='Yf'")->getField("GROUP_CONCAT(id)");
        $countArr['Yf'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //江门
        $inId = $exchangeModel->where("city_cd='Jm'")->getField("GROUP_CONCAT(id)");
        $countArr['Jm'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //阳江
        $inId = $exchangeModel->where("city_cd='Yg'")->getField("GROUP_CONCAT(id)");
        $countArr['Yj'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //茂名
        $inId = $exchangeModel->where("city_cd='Mm'")->getField("GROUP_CONCAT(id)");
        $countArr['Mm'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //湛江
        $inId = $exchangeModel->where("city_cd='Zj'")->getField("GROUP_CONCAT(id)");
        $countArr['Zj'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        
        //海南
        $inId = $exchangeModel->where("area_cd='Hk'")->getField("GROUP_CONCAT(id)");
        $countArr['Hk'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        //三亚
        $inId = $exchangeModel->where("area_cd='Sy'")->getField("GROUP_CONCAT(id)");
        $countArr['Sy'] = $exchange_codeModel->where("exchange_id IN ({$inId}) AND effective=1")->count();
        
        
        
        //剩下多少套
        $effective = $exchange_codeModel->where("effective=0")->count();
        
        
        $this->assign('countArr', $countArr); // 赋值分页输出
        $this->assign('effective', $effective+399); // 还有多少套
        $this->display('freeOfCharge');
    }
    
    /**
     * 距离我最近的页面
     */
    public function searchStores(){
		$userModel = M('User');
		$BanquetModel = M('Banquet');
        $wx_id = $this->userInfo['wx_id'];
        $whereBan = array('wx_id'=>$wx_id);
        $banqueData = $BanquetModel->where($whereBan)->find();
        $this->assign('banqueData', $banqueData); //js的json数据
		$this->assign('BanquetInfo', $banqueData); //宴会数据
        $this->assign('userInfo', $this->userInfo); //用户数据
		$whereToBanquet = "wx_id='{$wx_id}'";
		$BanquetInfo = $BanquetModel->where($whereToBanquet)->find();
		$whereToUser = array('wx_id'=>$BanquetInfo['wx_id']);
        $BanquetUserInfo = $userModel->where($whereToUser)->find();
		$this->assign('BanquetUserInfo', $BanquetUserInfo); //宴会主人数据
		
		
        $this->display('searchStores');
    }
    
    
    /**
     * 所有排行榜
     */
    public function rankingList(){
        $BanquetModel = M('Banquet');
		
		$userModel = M('User');
        $wx_id = $this->userInfo['wx_id'];
        $whereBan = array('wx_id'=>$wx_id);
        $banqueData = $BanquetModel->where($whereBan)->find();
        $this->assign('banqueData', $banqueData); //js的json数据
		$this->assign('BanquetInfo', $banqueData); //宴会数据
        $this->assign('userInfo', $this->userInfo); //用户数据
		$whereToBanquet = "wx_id='{$wx_id}'";
		$BanquetInfo = $BanquetModel->where($whereToBanquet)->find();
		$whereToUser = array('wx_id'=>$BanquetInfo['wx_id']);
        $BanquetUserInfo = $userModel->where($whereToUser)->find();
		//print_r($BanquetUserInfo);exit;
		$this->assign('BanquetUserInfo', $BanquetUserInfo); //宴会主人数据
		
		
		$wx_id = $this->userInfo['wx_id'];
		$whereBan = array('wx_id'=>$wx_id);
        $MeBanqueData = $BanquetModel->where($whereBan)->find();
        if($MeBanqueData['is_calculation']==1){
            $Url = \Org\Net\Fun::url("home/page/rankingSelf");
            header("Location:".$Url);exit;
        }
		
		
        $data = $BanquetModel->where("is_calculation=1 AND is_delete=0")->order('people DESC')->select();
        $this->assign('data', $data); // 排行榜数据
        $this->display('rankingList');
    }
    
    /**
     * 我的排行榜
     */
    public function rankingSelf(){
        $BanquetModel = M('Banquet');
		
		$userModel = M('User');
        $wx_id = $this->userInfo['wx_id'];
        $whereBan = array('wx_id'=>$wx_id);
        $banqueData = $BanquetModel->where($whereBan)->find();
        $this->assign('banqueData', $banqueData); //js的json数据
		$this->assign('BanquetInfo', $banqueData); //宴会数据
        $this->assign('userInfo', $this->userInfo); //用户数据
		$whereToBanquet = "wx_id='{$wx_id}'";
		$BanquetInfo = $BanquetModel->where($whereToBanquet)->find();
		$whereToUser = array('wx_id'=>$BanquetInfo['wx_id']);
        $BanquetUserInfo = $userModel->where($whereToUser)->find();
		$this->assign('BanquetUserInfo', $BanquetUserInfo); //宴会主人数据
		
		
        $wx_id = $this->userInfo['wx_id'];
		$whereBan = array('wx_id'=>$wx_id);
        $MeBanqueData = $BanquetModel->where($whereBan)->find();
        if($MeBanqueData['is_calculation']==0){
            $Url = \Org\Net\Fun::url("home/page/rankingList");
            header("Location:".$Url);exit;
        }
        
        
        $data = $BanquetModel->where("is_calculation=1 AND is_delete=0")->order('people DESC')->select();
        
		//我的排名
        $whrere = array();
        $whrere['wx_id'] = $this->userInfo['wx_id'];
        $meBanData = $BanquetModel->where($whrere)->find();

		
		//计算我的排名 start
        $mePeople = $meBanData['people'];
        $meScore = 0;
        $meScore = $BanquetModel->where("people > {$mePeople} AND is_calculation=1 AND is_delete=0")->count("id");
		if($data){
			foreach($data as $key=>$item){
				if($item['wx_id']==$wx_id){
					$meScore = $key+1;
				}
			}
		}
		//计算我的排名 end
		
        
        $this->assign('data', $data);   //排行榜数据
        $this->assign('meBanData', $meBanData);   //我的成绩数据
        $this->assign('meScore', $meScore);   //我在第几名
        
        $this->display('rankingSelf');
    }


    /**
     * 兑换号码 页面
     */
    public function Redeem(){
		$userModel = M('User');
		$BanquetModel = M('Banquet');
        $wx_id = $this->userInfo['wx_id'];
        $whereBan = array('wx_id'=>$wx_id);
        $banqueData = $BanquetModel->where($whereBan)->find();
        $this->assign('banqueData', $banqueData); //js的json数据
		$this->assign('BanquetInfo', $banqueData); //宴会数据
        $this->assign('userInfo', $this->userInfo); //用户数据
		$whereToBanquet = "wx_id='{$wx_id}'";
		$BanquetInfo = $BanquetModel->where($whereToBanquet)->find();
		$whereToUser = array('wx_id'=>$BanquetInfo['wx_id']);
        $BanquetUserInfo = $userModel->where($whereToUser)->find();
		$this->assign('BanquetUserInfo', $BanquetUserInfo); //宴会主人数据
		
        $this->display('Redeem');
    }
    
    /**
     * 验证兑换
     */
    public function verificationCode(){
        $exchange_codeModel = M('Exchange_code');
        $ReceiveObject = M('Receive');
        
        $tooken = $this->tooken;
        $postDataJson = $_POST['qwer'];
        $key = $_POST['session_id'];
        //print_r($_POST);exit;
        $returnData = array();
        if(md5($postDataJson.$this->tooken)!=$key){
            $returnData['message'] = '非法请求';
            $returnData['state'] = 0;
            echo json_encode($returnData);exit;
        } else {
            $psotDataObjec = json_decode($postDataJson);
            $code = $psotDataObjec->code; //代码
            $code = strtolower($code);

            //查找这个code是否存在
            $where = array();
            $where['code'] = $code;
            $dataInfo = $exchange_codeModel->where($where)->find();
            //print_r($dataInfo);exit;
            if(empty($dataInfo)){
                $returnData['message'] = '兑换码不存在';
                $returnData['state'] = 2;
                echo json_encode($returnData);exit;
            } else {
                
                //判断这个用户是否兑换过，如果兑换过，不能再进行兑换
//                $whereTore = array();
//                $whereTore['wx_id'] = $this->userInfo['wx_id'];
//                $reData = $ReceiveObject->where($whereTore)->find();
//                if(!empty($reData)){
//                    $returnData['message'] = '亲，您已经兑换过了哦';
//                    $returnData['state'] = 2;
//                    echo json_encode($returnData);exit;
//                }                
                
                //如果存在，则看下这个编码是否已经给兑换过了
                if($dataInfo['effective']==1){
                    $returnData['message'] = '该兑换码已经兑换过了';
                    $returnData['state'] = 2;
                    echo json_encode($returnData);exit;
                }
                
                //无错误信息，则进行兑换数据操作
                $saveData = array();
                $saveData['wx_id'] = $this->userInfo['wx_id'];
                $saveData['exchange_code_id'] = $dataInfo['id'];
                $saveData['exchange_code'] = $dataInfo['code'];
                $saveData['add_time'] = \Org\Net\Fun::now_time();
                $rid = $ReceiveObject->add($saveData);
                if($rid){
                    
                    //更新数据表,表识已经兑换过 begin
                    $updateData = array();
                    $updateData['effective'] = 1;
                    $whreUp = array();
                    $whreUp['id'] = $dataInfo['id'];
                    $exchange_codeModel->where($whreUp)->save($updateData);
                    //更新数据表,表识已经兑换过 end
                    
                    $returnData['message'] = '兑换成功';
                    $returnData['state'] = 1;
                    echo json_encode($returnData);exit;
                } else {
                    $returnData['message'] = '兑换失败';
                    $returnData['state'] = 0;
                    echo json_encode($returnData);exit;
                }
            }
        }
    }
    
}